1
|
|
|
/** |
2
|
|
|
* @package assets |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
(function($, Symphony) { |
6
|
|
|
'use strict'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Convert absolute to relative dates. |
10
|
|
|
* |
11
|
|
|
* @name $.symphonyTimeAgo |
12
|
|
|
* @class |
13
|
|
|
* |
14
|
|
|
* @param {Object} options An object specifying containing the attributes specified below |
15
|
|
|
* @param {String} [options.items='time'] Selector to find the absolute date |
16
|
|
|
* @param {String} [options.timestamp='utc'] Attribute of `object.items` representing the timestamp of the given date |
17
|
|
|
* @param {Integer} [options.max=0] Plugin will disable when the minutes exceed this value. By default this behaviour is off. |
18
|
|
|
* |
19
|
|
|
* @example |
20
|
|
|
|
21
|
|
|
$('.notifier').symphonyTimeAgo(); |
22
|
|
|
*/ |
23
|
|
|
$.fn.symphonyTimeAgo = function(options) { |
24
|
|
|
var objects = this, |
25
|
|
|
settings = { |
26
|
|
|
items: 'time', |
27
|
|
|
timestamp: 'utc', |
28
|
|
|
max: 0 |
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
$.extend(settings, options); |
32
|
|
|
|
33
|
|
|
/*-----------------------------------------------------------------------*/ |
34
|
|
|
|
35
|
|
|
Symphony.Language.add({ |
36
|
|
|
'just now': false, |
37
|
|
|
'a minute ago': false, |
38
|
|
|
'{$minutes} minutes ago': false, |
39
|
|
|
'about 1 hour ago': false, |
40
|
|
|
'about {$hours} hours ago': false |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
/*------------------------------------------------------------------------- |
44
|
|
|
Functions |
45
|
|
|
-------------------------------------------------------------------------*/ |
46
|
|
|
|
47
|
|
|
function parse(item) { |
48
|
|
|
var timestamp = item.data('timestamp'), |
49
|
|
|
datetime; |
50
|
|
|
|
51
|
|
|
// Fetch stored timestamp |
52
|
|
|
if($.isNumeric(timestamp)) { |
53
|
|
|
return timestamp; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Parse date |
57
|
|
|
else { |
|
|
|
|
58
|
|
|
datetime = item.attr(settings.timestamp); |
59
|
|
|
|
60
|
|
|
// Defined date and time |
61
|
|
|
if(datetime) { |
62
|
|
|
// Datetime will be in seconds since Epoch, JS requires |
63
|
|
|
// millseconds, so multiply by 1000. |
64
|
|
|
timestamp = new Date(datetime * 1000); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Undefined date and time |
68
|
|
|
else { |
69
|
|
|
timestamp = new Date().getTime(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Store and return timestamp |
73
|
|
|
item.data('timestamp', timestamp); |
74
|
|
|
return timestamp; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function say(from, to) { |
79
|
|
|
|
80
|
|
|
// Calculate time difference |
81
|
|
|
var distance = to - from, |
82
|
|
|
|
83
|
|
|
// Convert time to minutes |
84
|
|
|
time = Math.floor(distance / 60000); |
85
|
|
|
|
86
|
|
|
// Return relative date based on passed time |
87
|
|
|
if(time < 1) { |
88
|
|
|
return Symphony.Language.get('just now'); |
89
|
|
|
} |
90
|
|
|
if(time < 2) { |
91
|
|
|
return Symphony.Language.get('a minute ago'); |
92
|
|
|
} |
93
|
|
|
if(time < 45) { |
94
|
|
|
return Symphony.Language.get('{$minutes} minutes ago', { |
95
|
|
|
'minutes': time |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
if(time < 90) { |
99
|
|
|
return Symphony.Language.get('about 1 hour ago'); |
100
|
|
|
} |
101
|
|
|
else if (!settings.max || time < settings.max) { |
|
|
|
|
102
|
|
|
return Symphony.Language.get('about {$hours} hours ago', { |
103
|
|
|
'hours': Math.floor(time / 60) |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/*------------------------------------------------------------------------- |
109
|
|
|
Initialisation |
110
|
|
|
-------------------------------------------------------------------------*/ |
111
|
|
|
|
112
|
|
|
objects.find(settings.items).each(function timeago() { |
113
|
|
|
var item = $(this), |
114
|
|
|
from = parse(item), |
115
|
|
|
to = new Date(), |
116
|
|
|
rel = say(from, to); |
117
|
|
|
|
118
|
|
|
// Set relative time |
119
|
|
|
if (rel) { |
120
|
|
|
item.text(rel); |
121
|
|
|
} |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
/*-----------------------------------------------------------------------*/ |
125
|
|
|
|
126
|
|
|
return objects; |
127
|
|
|
}; |
128
|
|
|
|
129
|
|
|
})(window.jQuery, window.Symphony); |
130
|
|
|
|